package com.greyu.ysj.service.impl;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtInvestExample;
import com.greyu.ysj.entity.CbtRecharge;
import com.greyu.ysj.entity.CbtRechargeExample;
import com.greyu.ysj.entity.User;
import com.greyu.ysj.entity.UserExample;
import com.greyu.ysj.mapper.CbtRechargeMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtRechargeService;

@Service
@Transactional
public class CbtRechargeServiceImpl implements CbtRechargeService {
	@Autowired
	private CbtRechargeMapper cbtRechargeMapper;

	@Override
	public List<CbtRecharge> getRechargeListByuserId(Integer userId) {
		CbtRechargeExample example = new CbtRechargeExample();
		CbtRechargeExample.Criteria criteria = example.createCriteria();

        if (null != userId) {
            criteria.andUserIdEqualTo(userId);
        }       
        List<CbtRecharge> recharges = this.cbtRechargeMapper.selectByExample(example);
        if(recharges==null||recharges.size()<1) {
        	return null;
        }
        return recharges;
	}

	@Override
	public double getRechargeAmount(Integer userId, Integer coinId) {
		Double amount=this.cbtRechargeMapper.selectRechargeAmount(userId, coinId);
		System.out.println("amount=>"+amount);
		return amount;
	}

	@Override
	public ResultModel save(CbtRecharge cbtRecharge) {
		
		try {	
			if ( cbtRecharge.getCoinId()==null
					||cbtRecharge.getUserId()==null
					||cbtRecharge.getAmount()==null
	        		) {
	            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
	        }
			//insert
	        this.cbtRechargeMapper.insert(cbtRecharge);
		} catch (Exception e) {
			return ResultModel.error(ResultStatus.INSERT_ERROR);
		}
	        return ResultModel.ok(cbtRecharge);
		}

	/*@Override
	public List<CbtRecharge> getRechargeListId(Integer userId, Integer coinId) {
		
		CbtRechargeExample example = new CbtRechargeExample();
		CbtRechargeExample.Criteria criteria = example.createCriteria();

        if (null != userId) {
            criteria.andUserIdEqualTo(userId);
        }
        if (null != coinId) {
            criteria.andCoinIdEqualTo(coinId);
        }       
        List<CbtRecharge> recharges = this.cbtRechargeMapper.selectByExample(example);
        if(recharges==null||recharges.size()<1) {
        	return null;
        }
        return recharges;
	}*/

	@Override
	public List<CbtRecharge> getRechargeRecordByuserId(Integer page, Integer rows, Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		List<CbtRecharge> rechargeRecord=cbtRechargeMapper.selectRechargeRecordByuserId(userId);
		if(rechargeRecord==null || rechargeRecord.size()<1) {
			
			return null;
		}
		return rechargeRecord;
	}

	@Override
	public int totalPages(Integer userId, Integer coinId,Integer rows) {
		int total=this.cbtRechargeMapper.countRechargeRecord(userId, coinId);
		int totalPages;
		if(total%rows == 0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		System.out.println("totalPages_recharge=>"+totalPages);
		return totalPages;
	}

	@Override
	public List<CbtRecharge> drawMoneyList(Integer page, Integer rows, Integer userId,Integer coinId,String startTime,String endTime)throws ParseException {
		// TODO Auto-generated method stub
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		
		CbtRechargeExample example = new CbtRechargeExample();
		
		CbtRechargeExample.Criteria criteria = example.createCriteria();

		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		if(userId!=null){
			criteria.andUserIdEqualTo(userId);
		}
		if(null!=startTime && null!=endTime && !startTime.equals("") && !endTime.equals("")){
			criteria.andRechargeDateBetween(formatter.parse(startTime), formatter.parse(endTime));
		}
		if(null!=coinId){
			criteria.andCoinIdEqualTo(coinId);
		}
		criteria.andAmountGreaterThan(0.0);
		
        List<CbtRecharge> recharges = this.cbtRechargeMapper.selectByExample(example);
        if(recharges==null||recharges.size()<1) {
        	return null;
        }
        return recharges;
	
	}

	@Override
	public int ListTotal(Integer userId,Integer coinId, String startTime, String endTime, Integer rows) throws ParseException {
		// TODO Auto-generated method stub
		CbtRechargeExample example = new CbtRechargeExample();
		
		CbtRechargeExample.Criteria criteria = example.createCriteria();

		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		if(userId!=null){
			criteria.andUserIdEqualTo(userId);
		}
		if(null!=startTime && null!=endTime && !startTime.equals("") && !endTime.equals("")){
			criteria.andRechargeDateBetween(formatter.parse(startTime), formatter.parse(endTime));
		}
		if(null!=coinId){
			criteria.andCoinIdEqualTo(coinId);
		}
		criteria.andAmountGreaterThan(0.0);
		int total=(int) this.cbtRechargeMapper.countByExample(example);
		int totalPages;
		if(total%rows == 0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		System.out.println("totalPages_recharge=>"+totalPages);
		return totalPages;
	}

	@Override
	public double allDraw(Integer coinId, String startTime, String endTime) throws ParseException {
		// TODO Auto-generated method stub
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		Date data1 =null;
		Date data2 =null;
		
		if(null!=startTime && null!=endTime && !startTime.equals("") && !endTime.equals("")){
			 data1 = formatter.parse(startTime);
			 data2 = formatter.parse(endTime);
		}
		return cbtRechargeMapper.allDraw(coinId,data1, data2);
		
	}
}

